<?php

namespace SDXApi\Listeners;

/**
 * Created by PhpStorm.
 * User: Jerry
 * Date: 2017/4/10
 * Time: 13:59
 */
class LocationLinkSourceListener
{
	/**
	 * Created by Jerry
	 * @param null $link
	 * @return array|bool|mixed|stdClass
	 */
	public function onCallLink($link = null)
	{

		if (is_null($link)) {
			return false;
		}

		$data = $this->switchLink($link);

		return $data;
	}

	/**
	 * Created by Jerry
	 * @param null $link
	 * @return bool|mixed
	 */
	public function onCallIosLink($link = null)
	{
		if (is_null($link)) {
			return false;
		}

		return $this->iosLink($link);
	}

	/**
	 * Created by Jerry
	 * @param $link
	 * @return array|mixed|stdClass
	 */
	protected function switchLink($link)
	{
		$source = $this->sourceApp();

		$parameter = array();
		switch ($source) {
			case 1:
				$parameter = $this->androidLink($link);
				break;
			case 2:
				$parameter = $this->iosLink($link);
				break;
			case 3:
				$parameter = $this->h5Link($link);
				break;
		}

		return $parameter;
	}

	/**
	 * Created by Jerry
	 * @param $link
	 * @return mixed|stdClass|array
	 */
	protected function androidLink($link)
	{

	    $data = array();

		if (strpos($link, 'car')) {
			$data['link_new'] = $link;
			$urlLink = 'https://w.shidexian.com' . $link;
			$parseUrl = parse_url($urlLink);

			if (! empty($parseUrl['query'])) {
				$parameter = $this->convertUrlQuery($parseUrl['query']);
				$parameter = array_merge($parameter);
				$data['parameter'][] = $parameter;
			}

			$data['page_name'] = 'cart';
			$data['is_new_link'] = 1;
		}

		if (strpos($link, 'phone')) {
			$data['link_new'] = 'com.weechan.shidexianapp.activity.BindPhoneActivity';
			$urlLink = 'https://w.shidexian.com' . $link;
			$parseUrl = parse_url($urlLink);

			if (! empty($parseUrl['query'])) {
				$parameter = $this->convertUrlQuery($parseUrl['query']);
				$parameter = array_merge($parameter);
				$data['parameter'][] = $parameter;
			}

			$data['page_name'] = 'phone';
			$data['is_new_link'] = 1;
		}

		if (strpos($link, 'alb')) {
			$data['link_new'] = 'com.weechan.shidexianapp.activity.AlbumDetailActivity';
			$urlLink = 'https://w.shidexian.com' . $link;
			$parseUrl = parse_url($urlLink);

			if (! empty($parseUrl['query'])) {
				$parameter = $this->convertUrlQuery($parseUrl['query']);
				$data['parameter']['com.weechan.shidexianapp'] = $parameter['tid'];
			}

			$data['page_name'] = 'phone';
			$data['is_new_link'] = 1;
		}

        if (strpos($link, 'search')) {
            $data['link_new'] = 'com.weechan.shidexianapp.activity.OpenSearchGoodsActivity';
            $urlLink = 'https://w.shidexian.com' . $link;
            $parseUrl = parse_url($urlLink);

            if (! empty($parseUrl['query'])) {
                $parameter = $this->convertUrlQuery($parseUrl['query']);
                $data['parameter']['com.weechan.shidexianapp'] = $parameter['cid'];
            }

            $data['page_name'] = 'phone';
            $data['is_new_link'] = 1;
        }

        if (strpos($link, 'findInfos')) {
            $data['link_new'] = 'com.weechan.shidexianapp.presentation.ui.activity.ArticleDetailH5Activity';
            $urlLink = 'https://w.shidexian.com' . $link;
            $parseUrl = parse_url($urlLink);

            if (! empty($parseUrl['query'])) {
                $parameter = $this->convertUrlQuery($parseUrl['query']);
                $data['parameter']['com.weechan.shidexianapp'] = $parameter['comId'];
            }

            $data['page_name'] = 'phone';
            $data['is_new_link'] = 1;
        }

        if (strpos($link, 'spe')) {

            $data['link_new'] = 'com.weechan.shidexianapp.activity.SubjectDetailActivity';

            $linkData = explode('=', $link);
            $linkID = array_pop($linkData);

            if (! empty($linkID)) {
                $data['parameter']['com.weechan.shidexianapp'] = $linkID;
            }

            $data['page_name'] = 'spe';
            $data['is_new_link'] = 1;
        }

        if (strpos($link, 'alb')) {

            $data['link_new'] = 'com.weechan.shidexianapp.activity.AlbumDetailActivity';

            $linkData = explode('=', $link);
            $linkID = array_pop($linkData);

            if (! empty($linkID)) {
                $data['parameter']['com.weechan.shidexianapp '] = $linkID;
            }

            $data['page_name'] = 'alb';
            $data['is_new_link'] = 1;
        }

        if (strpos($link, 'home0')) {

            $data['link_new'] = 'com.weechan.shidexianapp.TabHostActivity';

            $data['page_name'] = 'home';
            $data['is_new_link'] = 1;
        }

        if (strpos($link, 'bag')) {

            $data['link_new'] = 'com.weechan.shidexianapp.activity.RedPackageActivity';

            $data['page_name'] = 'bag';
            $data['is_new_link'] = 1;
        }


		return $data;
	}

	/**
	 * Created by Jerry
	 * @param $link
	 * @return mixed
	 */
	protected function iosLink($link)
	{
        $data = array();

        if (strpos($link, 'info')) {
            $data['link_new'] = 'SDXGoodDetailViewController';
            $urlLink = 'https://w.shidexian.com' . $link;
            $parseUrl = parse_url($urlLink);

            if (! empty($parseUrl['query'])) {
                $parameter = $this->convertUrlQuery($parseUrl['query']);
                $parameter = array_merge($parameter);
                $data['parameter']['goods_id'] = $parameter['id'];
            }

        }

        return $data;
	}

	/**
	 * Created by Jerry
	 * @param $link
	 * @return array
	 */
	protected function h5Link($link)
	{
		$data = array();
        $user_id = I('post.uid', 0, 'int');

        if (strpos($link, 'phone')) {
            $phone = $this->getUserPhoneBind($user_id);

            if (empty($phone)) {
                $data['bind_phone'] = 0;
            }
            else{
                $data['bind_phone'] = 1;
            }
        }

		return $data;
	}

    /**
     * Created by Jerry
     * @param int $userId
     * @return bool
     */
    protected function getUserPhoneBind($userId = 0)
    {
        $phone = D('Users')->findUserPhone($userId);

        if (empty($phone)) {
            return false;
        }

        return true;
    }

	/**
	 * Created by Jerry
	 * @return int
	 */
	protected function sourceApp()
	{
		$is_app = I('post.is_app', 0, 'int');
		$is_ios = I('post.is_ios', 0, 'int');
		$is_h5 = I('post.is_h5', 0, 'int');

//		安卓
		if ($is_app && ($is_ios == 0) && ($is_h5 == 0)) {
			return 1;
		}

//		苹果
		if ($is_ios && ($is_app == 0) && ($is_h5 == 0)) {
			return 2;
		}

//		h5
		if ($is_h5 && ($is_ios == 0) && ($is_app == 0)) {
			return 3;
		}

		return 0;
	}

	/**
	 * Created by Jerry
	 * @param $query
	 * @return array
	 */
	static protected function convertUrlQuery($query)
	{
		$queryParts = explode('&', $query);
		$params = array();
		foreach ($queryParts as $param) {
			$item = explode('=', $param);
			$params[$item[0]] = $item[1];
		}
		return $params;
	}
}